package st.business.tepth.business.history.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.HistoryDetailBean;
import st.business.tepth.business.goodsorder.GoodsOrderDelegate;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-04 10:35
 * 历史详情
 */

public class HistoryDetailDelegate extends MvpFragment<HistoryDetailPresenter>
        implements HistoryDetailContract.View {
    public static HistoryDetailDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", id);
        HistoryDetailDelegate delegate = new HistoryDetailDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_right_button)
    TextView mTvAction;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.tv_bus_code)
    TextView mTvBusCode;
    @BindView(R2.id.tv_work_type)
    TextView mTvWorkType;
    @BindView(R2.id.tv_in_factory)
    TextView mTvInFactory;
    @BindView(R2.id.tv_out_time)
    TextView mTvOutTime;
    @BindView(R2.id.tv_driver)
    TextView mTvDriver;
    @BindView(R2.id.tv_work_step)
    TextView mTvStep;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private int mRepairId;
    private HistoryDetailAdapter mHistoryDetailAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public HistoryDetailPresenter getPresenter() {
        return new HistoryDetailPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_history_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("查看详情");

        mTvAction.setText("领料记录");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.setAdapter(mHistoryDetailAdapter =
                new HistoryDetailAdapter(null));
        mPresenter.getHistoryData(UserManager.getInstance().getToken(), mRepairId
        );
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @OnClick(R2.id.tv_right_button)
    public void onAction() {
        start(GoodsOrderDelegate.newInstance(mRepairId));
    }

    @Override
    public void showHistoryData(HistoryDetailBean historyDetailBean) {
        if (historyDetailBean != null) {
            mTvBusCode.setText(historyDetailBean.getVehicleNo());
            mTvDriver.setText(String.format("%s\n%s\n%s",
                    historyDetailBean.getDriverName(),
                    historyDetailBean.getDriverCode(),
                    historyDetailBean.getDriverMobileNo()));
            mTvWorkType.setText(historyDetailBean.getOrderTypeDesc());
            mTvInFactory.setText(historyDetailBean.getStartIncomingData());
            mTvOutTime.setText(historyDetailBean.getEndIncomingData());
            mTvStep.setText(historyDetailBean.getIncomingStateDesc());
            mHistoryDetailAdapter.setNewData(new HistoryDetailDataConvert()
                    .convert(historyDetailBean));
        }
    }
}